package com.aida.evil.eda.Welcome;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Login.Login;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/********************************************
 * 工程名：Aida
 * 包名：com.aida.evil.eda.Welcome
 * 时间：16-5-11 下午1:58
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：引导页
 ********************************************/
public class GuidePage extends AppCompatActivity
{
    /*引导页图片*/
    private static final int[] IMGRESS = {R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3};
    /*引导页标题*/
    private static final String[] TITLES = {"答题换流量", "题库丰富，易答易懂", "月底兑换流量"};
    /*引导页标题颜色*/
    private static final int[] TITLE_COLORS = {R.color.alphablue, R.color.alphared, R.color.alphaorange};
    /*引导页描述*/
    private static final String[] DESCS = {"不可以重复答题哦", "点击答题界面的图片，\n可以进入详情页寻找答案的说！", "每月月底将会统计您的成绩，\n次月初讲流量打入您的手机！"};


    private ViewPager viewpager;
    private Button button;

    private View[] views;/*viewpager内部的view*/
    private ViewPaperAdapter adapter;

    private int viewI = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guidepage);
        initView();/*初始化控件*/
        initViewPager();/*初始化ViewPager*/
        setViewpager();/*设置ViewPager*/
        onClick();/*点击事件*/
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        viewpager = (ViewPager) findViewById(R.id.guidepage_viewpager);
        button = (Button) findViewById(R.id.guidepage_button);
    }

    /**
     * 点击事件
     */
    public void onClick()
    {
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(new Intent(GuidePage.this, Login.class));
                finish();
            }
        });
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager()
    {
        ImageView[] imageViews = new ImageView[IMGRESS.length];
        TextView[] titles = new TextView[IMGRESS.length];
        TextView[] descs = new TextView[IMGRESS.length];
        views = new View[IMGRESS.length];

        for (viewI = 0; viewI < IMGRESS.length; viewI++)
        {
            /*填充布局*/
            views[viewI] = View.inflate(this, R.layout.guide_viewpager, null);

            /*设置图片*/
            imageViews[viewI] = ((ImageView) views[viewI].findViewById(R.id.guidepage_img));
            Picasso.with(this).load(IMGRESS[viewI]).into(imageViews[viewI]);

            imageViews[viewI].setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    viewpager.setCurrentItem(viewI);
                }
            });

            /*设置标题*/
            titles[viewI] = (TextView) views[viewI].findViewById(R.id.guidepage_title);
            titles[viewI].setText(TITLES[viewI]);
            titles[viewI].setTextColor(getResources().getColor(TITLE_COLORS[viewI]));

            /*设置描述*/
            descs[viewI] = (TextView) views[viewI].findViewById(R.id.guidepage_desc);
            descs[viewI].setText(DESCS[viewI]);
        }


    }

    /**
     * 设置ViewPager
     */
    private void setViewpager()
    {
        ArrayList<View> list = new ArrayList<>();
        for (int i = 0; i < views.length; i++)
        {
            list.add(views[i]);
        }
        adapter = new ViewPaperAdapter(list);

        viewpager.setOffscreenPageLimit(list.size());
        viewpager.setPageMargin(-dip2px(135));
        viewpager.setAdapter(adapter);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }

            @Override
            public void onPageSelected(int position)
            {
                if (position + 1 == viewpager.getAdapter().getCount())
                {
                    if (button.getVisibility() != View.VISIBLE)
                    {
                        button.setVisibility(View.VISIBLE);
                        button.startAnimation(AnimationUtils
                                .loadAnimation(GuidePage.this, android.R.anim.fade_in));
                    }
                }
                else
                {
                    if (button.getVisibility() != View.INVISIBLE)
                    {
                        button.setVisibility(View.INVISIBLE);
                        button.startAnimation(AnimationUtils
                                .loadAnimation(GuidePage.this, android.R.anim.fade_out));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });


        viewpager.setPageTransformer(false, new ViewPager.PageTransformer()
        {
            private static final float MIN_SCALE = 0.85f;

            private static final float MIN_TXT_SCALE = 0.0f;
            private static final float MIN_TXT_ALPHA = 0.0f;

            @SuppressLint("NewApi")
            @Override
            public void transformPage(View view, float position)
            {
                View mGuideImage = view.findViewById(R.id.guidepage_img);
                View mTitle = view.findViewById(R.id.guidepage_title);
                View mDesc = view.findViewById(R.id.guidepage_desc);

                int viewWidth = mDesc.getWidth();

                if (position < -1)
                {
                    mTitle.setAlpha(0);
                    mDesc.setAlpha(0);
                }
                else if (position <= 1)
                {
                    float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                    float scaleTxtFactor = Math.max(MIN_TXT_SCALE, 1 - Math.abs(position));

                    mGuideImage.setScaleX(scaleFactor);
                    mGuideImage.setScaleY(scaleFactor);

                    mTitle.setScaleX(scaleTxtFactor);
                    mTitle.setScaleY(scaleTxtFactor);
                    mTitle.setAlpha(MIN_TXT_ALPHA + (scaleTxtFactor - MIN_TXT_SCALE) / (1 - MIN_TXT_SCALE) * (1 - MIN_TXT_ALPHA));

                    mDesc.setAlpha(mTitle.getAlpha());
                    mDesc.setScaleX(scaleTxtFactor);
                    mDesc.setScaleY(scaleTxtFactor);
                }
                else
                {
                    mTitle.setAlpha(0);
                    mDesc.setAlpha(0);
                }
            }
        });

    }

    private int dip2px(float dpValue)
    {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /***************************************
     * 内部类
     */

    private class ViewPaperAdapter extends PagerAdapter
    {
        private ArrayList<View> views;

        public ViewPaperAdapter(ArrayList<View> views)
        {
            this.views = views;
        }

        @Override
        public int getCount()
        {
            if (views != null)
            {
                return views.size();
            }
            else
                return 0;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return (arg0 == arg1);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            container.removeView(views.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            container.addView(views.get(position), 0);
            return views.get(position);
        }
    }

}
